package com.movecar.ui;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.movecar.R;
import com.movecar.model.BankListBean;
import com.movecar.model.GroupBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.RequestResultCallback;
import com.movecar.widget.CustomProgressDialog;

/**
 * 云豆提现银行列表
 * 
 * @author Zhao.JQ
 * 
 */
public class CloudCoinsBankListActivity extends Activity implements OnClickListener, RequestResultCallback,
		OnItemClickListener {

	private ListView mListView;
	private List<BankListBean> mDataList = new ArrayList<BankListBean>();
	private List<BankListBean> resultList = new ArrayList<BankListBean>();
	private CustomProgressDialog progressDialog = null;
	private DataAdapter dataAdapter;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.bank_list);
		dataAdapter = new DataAdapter(CloudCoinsBankListActivity.this);
		initView();
	}

	private void initView() {

		// 活动列表
		mListView = (ListView) findViewById(R.id.mListView);
		Button btn_left = (Button) findViewById(R.id.btn_left);

		TextView title = (TextView) findViewById(R.id.title);
		title.setText("银行");
		btn_left.setVisibility(View.VISIBLE);
		startProgressDialog();
		btn_left.setOnClickListener(this);
		startProgressDialog();
		getBankList();

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_left:
			finish();
			break;

		default:
			break;
		}

	}

	/**
	 * 获取银行列表
	 */
	@SuppressWarnings("unchecked")
	private void getBankList() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "queryBankListNew1")); // 添加要传递的参数
		new HttpPostRequestAsyncTask(CloudCoinsBankListActivity.this, GroupBean.class, AppConfig.USER).execute(params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onSuccess(Object object) {

		stopProgressDialog();

		Type type = new TypeToken<List<BankListBean>>() {
		}.getType();
		Gson gson = new Gson();
		String result = (String) object;
		Object fromJson = gson.fromJson(result, type);

		try {
			mDataList = (List<BankListBean>) fromJson;
		} catch (Exception e) {
			return;
		}

		if (mDataList != null) {

			resultList.clear();
			for (BankListBean bean : mDataList) {

				resultList.add(bean);
			}

			dataAdapter = new DataAdapter(CloudCoinsBankListActivity.this);
			mListView.setAdapter(dataAdapter);
			mListView.setOnItemClickListener(this);

		} else {
			Toast.makeText(getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onFail(String exception) {

		stopProgressDialog();
		dataAdapter = new DataAdapter(CloudCoinsBankListActivity.this);

		Alert.error(CloudCoinsBankListActivity.this, "与服务器连接失败，请检查网络");

	}

	class DataAdapter extends BaseAdapter {

		private Activity activity;
		private LayoutInflater inflater;
		private ViewHolder viewHolder = null;

		DataAdapter(Activity activity) {

			this.activity = activity;
			inflater = LayoutInflater.from(activity);
		}

		@Override
		public int getCount() {

			return resultList.size();
		}

		@Override
		public Object getItem(int arg0) {

			return resultList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {

			return arg0;
		}

		@Override
		public View getView(final int position, View view, ViewGroup arg2) {

			final ViewHolder viewHolder;

			if (view == null) {
				viewHolder = new ViewHolder();
				view = inflater.inflate(R.layout.bank_list_item, null);
				viewHolder.bank_list_ralative = (RelativeLayout) view.findViewById(R.id.bank_list_ralative);
				viewHolder.bank_name = (TextView) view.findViewById(R.id.bank_name);

				view.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) view.getTag();
			}

			// 字体加粗
			TextPaint tp = viewHolder.bank_name.getPaint();
			tp.setFakeBoldText(true);
			viewHolder.bank_name.setText(resultList.get(position).getBankName());

			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(CloudCoinsBankListActivity.this, CloudCoinsBankRechargeActivity.class);
					intent.putExtra("bankId", resultList.get(position).getBankId());
					startActivityForResult(intent, 0);

				}
			});
			return view;
		}

		class ViewHolder {
			RelativeLayout bank_list_ralative;
			TextView bank_name;
		}
	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				finish();
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

	}

}
